package view.backend;

import exceptionManager.ExceptionManager;
import java.awt.Font;
import java.awt.event.*;
import java.sql.SQLException;
import model.handlers.ProgramValueHandler;
import javax.swing.*;
import view.generaldesign.DesignLayout;

/**
 *  Denne klasse er skærmbilledet til ændring af ProgramValue, som inkluderer Moms og Miljøafgift
 * @author Sonny Sandberg
 */
public class AdminProgramValues
{
    private JPanel valuePanel;
    private model.ProgramValue valueObject;
    private JTextField
            tFieldVAT,
            tFieldETax;
    
    public AdminProgramValues()
    {
        try
        {
            valueObject = ProgramValueHandler.getCurrentProgramValueModelObject();
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
        
        valuePanel = new JPanel(null);
        valuePanel.setBounds(395, 320, 650, 500);
        valuePanel.setOpaque(false);
        
        JLabel lblVAT = new JLabel("Moms");
        lblVAT.setBounds(10, 25, 150, 18);
        lblVAT.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        JLabel lblETax = new JLabel("Miljøafgift");
        lblETax.setBounds(10, 80, 150, 18);
        lblETax.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        tFieldVAT = new JTextField();
        tFieldVAT.setText(""+valueObject.getVat());
        tFieldVAT.setBounds(10, 45, 100, 25);
        tFieldVAT.setBorder(DesignLayout.T_FIELD_RED);
        tFieldVAT.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldVAT.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldVAT.setFont(DesignLayout.STANDARD);
        tFieldVAT.addFocusListener(new FocusListener() 
        {
            @Override
            public void focusGained(FocusEvent e)
            {
                tFieldVAT.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                tFieldVAT.select(0, 0);
            }
        });
        tFieldVAT.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                try
                {
                    valueObject.setVat(Double.parseDouble(tFieldVAT.getText()));
                } 
                catch (NumberFormatException evt)
                {
                    tFieldVAT.setText(""+valueObject.getVat());
                }
            }
        });
        
        tFieldETax = new JTextField();
        tFieldETax.setText(""+valueObject.getEnvironment_tax());
        tFieldETax.setBounds(10, 100, 100, 25);
        tFieldETax.setBorder(DesignLayout.T_FIELD_RED);
        tFieldETax.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldETax.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldETax.setFont(DesignLayout.STANDARD);
        tFieldETax.addFocusListener(new FocusListener() 
        {
            @Override
            public void focusGained(FocusEvent e)
            {
                tFieldETax.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                tFieldETax.select(0, 0);
            }
        });
        tFieldETax.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                try
                {
                    valueObject.setEnvironment_tax(Double.parseDouble(tFieldETax.getText()));
                } 
                catch (NumberFormatException evt)
                {
                    tFieldETax.setText(""+valueObject.getEnvironment_tax());
                }                
            }
        });
        
        JButton btnUpdate = new JButton("opdater");
        btnUpdate.setBounds(10, 150, 100, 35);
        btnUpdate.requestFocus(true);
        btnUpdate.setBorder(DesignLayout.T_FIELD_RED);
        btnUpdate.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnUpdate.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                int n = JOptionPane.showConfirmDialog(
                null,
                "Ønsker du at ændre Moms / Miljøafgift i Systemet?",
                "Bekræft Venligst",
                JOptionPane.YES_NO_OPTION);
                if (n == 0)
                {
                    try
                    {
                        // Brugeren har bekræftet, at oplysningerne skal ændres.
                        int numAdded = control.ProgramValueControl.assignNewProgramValues(valueObject);
                        if (numAdded > 0)
                        {
                            valueObject = ProgramValueHandler.getCurrentProgramValueModelObject();
                            tFieldVAT.setText(""+valueObject.getVat());
                            tFieldETax.setText(""+valueObject.getEnvironment_tax());
                        }
                    } 
                    catch (SQLException ex)
                    {
                        ExceptionManager.handleSQLException(ex);
                    } 
                    catch (ClassNotFoundException ex)
                    {
                        ExceptionManager.handleClassNotFoundException(ex);
                    }
                }
            }
        });
        
        valuePanel.add(lblVAT);
        valuePanel.add(lblETax);
        valuePanel.add(tFieldVAT);
        valuePanel.add(tFieldETax);
        valuePanel.add(btnUpdate);
        valuePanel.setVisible(false);
    }

    public JPanel getPanel()
    {
        return valuePanel;
    }
}